package shuashuami.hb.com.view;

import android.content.Context;
import android.text.Layout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.listener.IMenuBtnClickListener;
import shuashuami.hb.com.manager.HbAppManager;
import shuashuami.hb.com.model.HbMenuInfo;

/**
 * <b>类   名：</b>${TYPE_NAME} <br/>
 * <b>类描述：</b> <br/>
 * <b>创建人：</b>zhangqi<br/>
 * <b>创建时间：</b>2017/6/13<br/>
 * <b>修改人：</b>zhangqi<br/>
 * <b>修改时间：</b>2017/6/13<br/>
 * <b>修改备注：</b><br/>
 *
 * @version 1.0.0<br/>
 */
public class HbBottomBar extends RelativeLayout {
    LayoutInflater mInflater;
    Context mContext;

    IMenuBtnClickListener listener;

    LinearLayout parent;
    public HbBottomBar(Context context) {
        super(context);
        this.mContext=context;
        initView();

    }

    public HbBottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext=context;
        initView();
    }

    public HbBottomBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;
        initView();
    }

    public void registerClickListener(IMenuBtnClickListener listener){
        this.listener=listener;

    }



    public void initView(){
        mInflater=LayoutInflater.from(mContext);
        LinearLayout bottom= (LinearLayout) mInflater.inflate(R.layout.ui_buttom_bar,null);
        LayoutParams params=new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );
        bottom.setLayoutParams(params);
        addView(bottom);



        parent= (LinearLayout) bottom.findViewById(R.id.button_parent);

    }

    public void setSelect(boolean isSelect,ViewGroup group){

        int tag=Integer .parseInt(group.getTag().toString());
        HbMenuInfo hbMenuInfo= HbAppManager.getInstance().getMenuInfos().get(tag);
        if(!isSelect){
            ((ImageView)group.getChildAt(0)).setImageResource(hbMenuInfo.getmIcon_nor());
            ((TextView)group.getChildAt(1)).setTextColor(0xf0cccccc);

        }else{
            ((ImageView)group.getChildAt(0)).setImageResource(hbMenuInfo.getmIcon_sel());
            ((TextView)group.getChildAt(1)).setTextColor(getResources().getColor(R.color.app_main));

        }

    }





    public void addMenuItem(HbMenuInfo hbMenuInfo) {

        LinearLayout v= (LinearLayout)mInflater.inflate(R.layout.ui_button_bar_item,null);
        v.setTag(parent.getChildCount());
        parent.addView(v);

        final LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT,1.0f);
        v.setLayoutParams(params);

        if(parent.getChildCount()==1){
            ((ImageView) v.getChildAt(0)).setImageResource(hbMenuInfo.getmIcon_sel());
            ((TextView) v.getChildAt(1)).setText(hbMenuInfo.getmName());
            ((TextView)v.getChildAt(1)).setTextColor(getResources().getColor(R.color.app_main));

        }else {
            ((ImageView) v.getChildAt(0)).setImageResource(hbMenuInfo.getmIcon_nor());
            ((TextView) v.getChildAt(1)).setText(hbMenuInfo.getmName());
        }

        v.setClickable(true);
        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener!=null){
                    for(int i=0;i<parent.getChildCount();i++){
                        setSelect(false,(LinearLayout)parent.getChildAt(i));

                    }
                    setSelect(true,(LinearLayout)view);

                    listener.onMenuBtnClick(Integer.parseInt(view.getTag().toString()));

                }
            }
        });




    }
}
